package com.hk.service.Impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hk.constant.CacheConstants;
import com.hk.constant.HttpStatus;
import com.hk.core.PageResult;
import com.hk.domain.Dict;
import com.hk.dto.DictListDto;
import com.hk.mapper.DictMapper;
import com.hk.service.DictService;
import com.hk.vo.DictVo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {
private final Integer  DEFAULT_PAGE_SIZE = 10;
private final Integer  DEFAULT_PAGE_NUM = 1;

    @Override
    public void saveDict(DictVo dictVo) {
        Dict dict = new Dict();
        BeanUtils.copyProperties(dictVo, dict);

        super.save(dict);
    }

    @Override
    public PageResult dictlist(DictListDto dictListDto) {
        if (ObjectUtils.isEmpty(dictListDto.getPageNum())){
            dictListDto.setPageNum(DEFAULT_PAGE_NUM);
            dictListDto.setPageSize(DEFAULT_PAGE_SIZE);
        }

        PageResult pageResult = new PageResult();
        Page<Dict> page = new Page<>(dictListDto.getPageNum(),  dictListDto.getPageSize());
        LambdaQueryWrapper<Dict> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(ObjectUtils.isNotEmpty(dictListDto.getDictName()),Dict::getDictName, dictListDto.getDictName());
        wrapper.like(ObjectUtils.isNotEmpty(dictListDto.getDictType()),Dict::getDictType, dictListDto.getDictType());
        wrapper.eq(ObjectUtils.isNotEmpty(dictListDto.getStatus()),Dict::getStatus, dictListDto.getStatus());
        if(ObjectUtils.isNotEmpty(dictListDto.getParams())) {
            wrapper.between(ObjectUtils.isNotEmpty(dictListDto.getParams()), Dict::getCreateTime, dictListDto.getParams().get("beginTime"), dictListDto.getParams().get("endTime"));
        }
        page = super.page(page, wrapper);
         pageResult.setCode(HttpStatus.SUCCESS);
         pageResult.setMsg(CacheConstants.LOGIN_SUCCESS1);
         pageResult.setRows(page.getRecords());
         pageResult.setTotal(Integer.parseInt(page.getTotal()+""));

        return pageResult;
    }

    @Override
    public void delectDict(List<Integer> ids) {
        super.removeBatchByIds(ids);
    }

    @Override
    public void updateDict(DictVo dictVo) {
        Dict dict = new Dict();
        BeanUtils.copyProperties(dictVo, dict);
        super.updateById(dict);
    }
}

